home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Src / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  12.3 KB  |  577 lines

  1. /*
  2.  * $Id: io.c 1.3 1994/09/09 12:31:30 digulla Exp digulla $
  3.  *
  4.  * $Log: io.c $
  5.  * Revision 1.3  1994/09/09  12:31:30  digulla
  6.  * added new style Prototypes, DEFCMD and DEFHELP
  7.  *
  8.  * Revision 1.2  1994/08/19  14:10:47  digulla
  9.  * Incorporated code by B.Noll. Fixes some problems with icons
  10.  *
  11.  * Revision 1.1  1994/08/13  16:36:27  digulla
  12.  * Initial revision
  13.  *
  14.  *
  15.  */
  16.  
  17. #include "defs.h"
  18. #include "rexx.h"
  19.  
  20. typedef struct WBStartup    WBS;
  21. typedef struct DiskObject   DISKOBJ;
  22.  
  23.  
  24. /*DEFHELP #cmd io NEWFILE name - replace current text with new file */
  25. /*DEFHELP #cmd textedit,io INSFILE name - insert a file into the current text. */
  26.  
  27. DEFUSERCMD("newfile", 1, CF_VWM, int, do_edit, (void),;)
  28. DEFUSERCMD("insfile", 1,      0, int, do_edit, (void),)
  29. {
  30.     FILE *  fi;
  31.     BPTR    oldlock;
  32.     BPTR    new_lock;
  33.     LONG    lines;
  34.     UBYTE   buf[MAXLINELEN];
  35.     LINE    ptr;
  36.     BOOL    failed = 1;
  37.     BOOL    newfile = (av[0][0] == 'n');
  38.     ED      * ep       = Ep;
  39.  
  40.     text_sync ();
  41.  
  42.     if (newfile)
  43.     {    /* newfile or insfile */
  44. DEFMESSAGE( __xdme_warning, "XDME Warning" )
  45.     if (GETF_MODIFIED(ep) && getyn(__xdme_warning,
  46. DEFMESSAGE( __delete_modified_text, "Delete modified text?" )
  47.         __delete_modified_text,
  48. DEFMESSAGE( __ok_cancel, "Yes|No" )
  49.         __ok_cancel) == 0)
  50.         return (-1);
  51.  
  52.     ep = uninit_init (ep);
  53.  
  54.     strncpy ((char *)ep->name, (char *)av[1], 63);
  55.     ep->name[63] = 0;
  56.     SETF_MODIFIED(ep,0);
  57.     }
  58.     else
  59.     {
  60.     SETF_MODIFIED(ep,1);
  61.     }
  62.  
  63.     lines = ep->lines;
  64.     oldlock = CurrentDir (ep->dirlock);
  65.  
  66.     if (fi = fopen (av[1], "r"))
  67.     {
  68.     int  len;
  69.     char oktitle  = 1;
  70.  
  71.     /* Get lock for file */
  72.     if (newfile)
  73.     {
  74.         if (new_lock = Lock (av[1], SHARED_LOCK))
  75.         {
  76.         BOOL success;
  77.  
  78.         /* get full path */
  79.         success = NameFromLock (new_lock, tmp_buffer,
  80.                               sizeof (tmp_buffer));
  81.  
  82.         /* always unlock lock */
  83.         UnLock (new_lock);
  84.  
  85.         if (success)
  86.         {
  87.             strncpy (ep->name, FilePart (tmp_buffer),
  88.                               sizeof (ep->name)-1);
  89.             ep->name[sizeof(ep->name)-1] = 0;
  90.  
  91.             *(PathPart(tmp_buffer)) = 0;
  92.  
  93.             new_lock = Lock (tmp_buffer, SHARED_LOCK);
  94.  
  95.             UnLock (ep->dirlock);
  96.  
  97.             ep->dirlock = new_lock;
  98.         }
  99.         }
  100.     }
  101.  
  102. DEFMESSAGE( __loading, "Loading %s ..." )
  103.     title (__loading, ep->name);
  104.  
  105.     while ((len = xefgets(fi, (char *)buf, MAXLINELEN-1)) >= 0)
  106.     {
  107.         failed = 0;
  108.         len ++;
  109.  
  110.         if (makeroom(256) && (ptr = allocline(len)))
  111.         {
  112.         SETLINE(ep,ep->lines,ptr);
  113.         ep->lines ++;
  114.  
  115.         movmem(buf, ptr, len);
  116.         } else
  117.         {
  118.         set_window_params ();
  119.         nomemory ();
  120.         oktitle = 0;
  121.         break;
  122.         }
  123.     }
  124.  
  125.     set_window_params ();
  126.  
  127.     if (oktitle)
  128.     {
  129. DEFMESSAGE( __loading_ok, "Loading OK" )
  130.         title (__loading_ok);
  131.         SETF_MFORCETITLE(ep,TRUE);
  132.     }
  133.  
  134.     fclose (fi);
  135.     } else /* couldn't open file */
  136.     {
  137.     if (!newfile) /* insertfile ? */
  138.     {
  139. DEFMESSAGE( __file_not_found, "%s:\nFile `%s'\nnot found" )
  140.         warn (__file_not_found, av[0], av[1]);
  141.  
  142.         cmderr = CMD_FAILED;
  143.     } else
  144.     {
  145.         /* copy argument */
  146.         strcpy (tmp_buffer, av[1]);
  147.  
  148.         /* copy name of file */
  149.         strncpy (ep->name, FilePart (tmp_buffer), sizeof (ep->name)-1);
  150.         ep->name[sizeof(ep->name)-1] = 0;
  151.  
  152.         /* clip name off */
  153.         *(PathPart (tmp_buffer)) = 0;
  154.  
  155.         /* get new lock */
  156.         new_lock = Lock (tmp_buffer, SHARED_LOCK);
  157.  
  158.         /* unlock old lock */
  159.         UnLock (ep->dirlock);
  160.  
  161.         /* remember new lock */
  162.         ep->dirlock = new_lock;
  163.     }
  164.     }
  165.  
  166.     CurrentDir (oldlock);
  167.  
  168.     if (ep->lines != 1 && lines == 1 && *GETTEXT(ep,0) == 0)
  169.     {
  170.     ep->line     = 0;
  171.     freeline (GETLINE(ep,0));
  172.     bmovl (ep->list+1, ep->list, --ep->lines);
  173.     } else
  174.     {
  175.     if (!failed && lines <= ep->lines - 1)
  176.     {
  177.         Block block;
  178.  
  179.         /* INSFILE: move text to correct position */
  180.         block.ep         = ep;
  181.         block.start_line = lines;
  182.         block.end_line   = ep->lines - 1;
  183.         block.type         = BT_LINE;
  184.  
  185.         displayblock (0);
  186.         set_block (&block);
  187.  
  188.         do_bmove ();
  189.     }
  190.     }
  191.  
  192.     set_window_params ();
  193.     window_title ();
  194.     text_load ();
  195.     text_redisplay ();
  196.  
  197.     return (!failed);
  198. } /* do_edit */
  199.  
  200.  
  201. static BOOL blockmode;
  202.  
  203. /*DEFHELP #cmd io APPENDSAVE file - Append current text to @{B}file@{UB} */
  204.  
  205. DEFUSERCMD("appendsave", 1, CF_VWM|CF_COK|CF_ICO, int, do_appendsave, (void),)
  206. {
  207.     return (saveit ("a"));
  208. } /* do_appendsave */
  209.  
  210.  
  211. /*DEFHELP #cmd io,block BAPPENDSAVE file - Append currently marked text to @{B}file@{UB} */
  212.  
  213. DEFUSERCMD("bappendsave", 1, CF_VWM|CF_COK|CF_ICO, int, do_bappendsave, (void),)
  214. {
  215.     int result = FALSE;
  216.  
  217.     if (block_ok ())
  218.     {
  219.     blockmode = 1;
  220.  
  221.     result = saveit ("a");
  222.  
  223.     blockmode = 0;
  224.     } else
  225. DEFMESSAGE( __no_block_specified, "%s:\nNo block specified" )
  226.     error (__no_block_specified, av[0]);
  227.  
  228.     return (result);
  229. } /* do_bappendsave */
  230.  
  231.  
  232. /*DEFHELP #cmd block,io BSAVE file - save the block to @{B}file@{UB} */
  233.  
  234. DEFUSERCMD("bsave", 1, CF_VWM|CF_ICO, int, do_bsave, (void),)
  235. {
  236.     int result = FALSE;
  237.  
  238.     if (block_ok ())
  239.     {
  240.     blockmode = 1;
  241.  
  242.     result = saveit ("w");
  243.  
  244.     blockmode = 0;
  245.     } else
  246. DEFMESSAGE( __no_block_specified, "%s:\nNo block specified" )
  247.     error (__no_block_specified, av[0]);
  248.  
  249.     return (result);
  250. } /* do_bsave() */
  251.  
  252.  
  253. /*DEFHELP #cmd block,requester,io BARPSAVE - save the block to a file with filerequester */
  254.  
  255. DEFUSERCMD("barpsave", 0, CF_VWM|CF_ICO, int, do_barpsave, (void),)
  256. {
  257.     int result = FALSE;
  258.  
  259.     if (block_ok ())
  260.     {
  261.     blockmode = 1;
  262.  
  263.     result = aslsave ();
  264.  
  265.     blockmode = 0;
  266.     } else
  267. DEFMESSAGE( __no_block_specified, "%s:\nNo block specified" )
  268.     error (__no_block_specified, av[0]);
  269.  
  270.     return (result);
  271. } /* do_barpsave */
  272.  
  273.  
  274. /*DEFHELP #cmd io SAVEOLD - save current text under current name */
  275.  
  276. DEFUSERCMD("saveold", 0, CF_VWM|CF_COK|CF_ICO, int, do_save, (void),)
  277. {
  278.     av[1] = Ep->name;
  279.  
  280.     return (do_writeto ());
  281. } /* do_save() */
  282.  
  283.  
  284. /*DEFHELP #cmd io SAVEAS file - save current text under a different name (title line name does change) */
  285.  
  286. DEFUSERCMD("saveas", 1, CF_VWM|CF_COK|CF_ICO, int, do_saveas, (void),)
  287. {
  288.     int ret = saveit ("w");
  289.  
  290.     if (!blockmode) /* PATCH_NULL 10-11-94 : added */
  291.     do_chfilename ();
  292.  
  293.     return (ret);
  294. } /* do_saveas */
  295.  
  296.  
  297. /*DEFHELP #cmd io WRITETO file - write text to this file. The current name of the text is not changed. */
  298.  
  299. DEFUSERCMD("writeto", 1, CF_VWM|CF_COK|CF_ICO, int, do_writeto, (void),)
  300. {
  301.     return (saveit ("w"));
  302. } /* do_writeto */
  303.  
  304.  
  305. /*DEFHELP #cmd requester,io ARPSAVE - SAVEAS with filerequester */
  306.  
  307. DEFUSERCMD("arpsave", 0, CF_VWM|CF_ICO, int, do_aslsave, (void),)
  308. {
  309.     return (aslsave ());
  310. } /* do_aslsave */
  311.  
  312.  
  313. /*DEFHELP requester,io REQSAVE - SAVEAS with ReqTools filerequester */
  314.  
  315. DEFUSERCMD("reqsave", 0, CF_VWM|CF_ICO, int, do_reqsave, (void),)
  316. {
  317.     return (reqsave ());
  318. } /* do_reqsave */
  319.  
  320.  
  321. Prototype int getappname (char * buffer, int bsize);
  322.  
  323. int getappname (char * buffer, int bsize)
  324. {
  325.     char   buff2[108];
  326.     char * pn ;
  327.     BPTR   pd_lock;
  328.  
  329.     pd_lock = GetProgramDir();
  330.  
  331.     if (!Wbs) {
  332.     /* pd_lock = GetProgramDir(); */
  333.  
  334.     if (!GetProgramName (buff2, sizeof (buff2))) {
  335.         // ERROR (size) or WB-Start or s.o. did set it name NULL
  336.         return FALSE;
  337.     } /* if */
  338.     pn = buff2;
  339.      } else {
  340.     /* pd_lock = ((struct WBStartup *)Wbs)->sm_ArgList[0].wa_Lock; */
  341.     pn = ((struct WBStartup *)Wbs)->sm_ArgList[0].wa_Name;
  342.     } /* if (!)wbs */
  343.  
  344. /* printf ("PName: %s\n", pn); */
  345.  
  346.     if (pd_lock) {
  347.        if (!NameFromLock (pd_lock, buffer, bsize)) {
  348.        // ERROR (size or path not mounted)
  349.        return FALSE;
  350.     } /* if */
  351.  
  352. /* printf ("PDir:  %s ($%08lx)\n", buffer, pd_lock); */
  353.  
  354.     if (!AddPart (buffer, FilePart(pn), bsize)) {
  355.         // ERROR (size)
  356.         return FALSE;
  357.     } /* if */
  358.     } else
  359.     strncpy (buffer, pn, bsize);
  360.  
  361. /* printf ("PPath: %s\n", buffer); */
  362.     return TRUE;
  363. } /* getappname */
  364.  
  365.  
  366. Prototype int saveit (const char * om);
  367.  
  368. int saveit (const char * om)
  369. {
  370.     BPTR    oldlock;
  371.     FILE  * fi;
  372.     LONG    i;
  373.     WORD    j,
  374.         k;
  375.     UBYTE * ptr,
  376.       * bp,
  377.         buffer[MAXLINELEN];
  378.     Line    xs,
  379.         xe;
  380.     ED      * ep;
  381.  
  382. DEFMESSAGE( __unnamed, "unnamed" )
  383.     if (!stricmp (av[1], __unnamed))
  384.     {
  385.     return (do_aslsave ());
  386. DEFMESSAGE( __unnamed, "unnamed" )
  387.     } else if (!stricmp (Ep->name, __unnamed))  /* get new name */
  388.     {
  389.     strncpy (Ep->name, FilePart (av[1]), sizeof (Ep->name)-1);
  390.     Ep->name[sizeof(Ep->name)-1] = 0;
  391.     }
  392.  
  393.     if (blockmode) /* block is ok */
  394.     {
  395.     xs = ActualBlock.start_line;
  396.     xe = ActualBlock.end_line + 1;
  397.     ep = ActualBlock.ep;
  398.     } else
  399.     {
  400.     xs = 0;
  401.     xe = Ep->lines;
  402.     ep = Ep;
  403.     }
  404.  
  405.     text_sync ();
  406.     oldlock = CurrentDir (Ep->dirlock);
  407.  
  408.     if (GETF_DOBACK(Ep))
  409.     {
  410.     BPTR lock;
  411.  
  412.     lock = Lock (av[1], ACCESS_READ);
  413.  
  414.     if (lock)
  415.     {
  416.         UnLock (lock);
  417.         sprintf (buffer, "%s.bak", av[1]);
  418.         Rename (av[1], buffer);
  419.     }
  420.     }
  421.  
  422.     if (fi = fopen (av[1], om))
  423.     {
  424. DEFMESSAGE( __saving, "Saving ..." )
  425.     title (__saving);
  426.  
  427.     for (i = xs; i < xe; ++i)
  428.     {
  429.         ptr = GETTEXT(ep,i);
  430.  
  431.         if (GETF_SAVETABS(ep))
  432.         {
  433.         for (bp = buffer, j = 0; *ptr; ++ptr, ++bp, j = (j+1)&7)
  434.         {
  435.             *bp = *ptr;
  436.             if (j == 7 && *bp == ' ' && *(bp-1) == ' ')
  437.             {
  438.             k = j;
  439.             while (k-- >= 0 && *bp == ' ')
  440.                 --bp;
  441.             *++bp = 9;
  442.             } else
  443.             {
  444.             if (GETF_SIMPLETABS(ep))        /* PATCH_NULL [25 Jan 1993] : line added */
  445.                 break;            /* PATCH_NULL [25 Jan 1993] : line added */
  446.  
  447.             if (*bp < 32 || *bp == '\"' || *bp == '\'' || *bp == '`' || *bp == '(')
  448.                 break;
  449.             }
  450.         }
  451.         strcpy ((char *)bp, (char *)ptr);
  452.  
  453.         ptr = buffer;
  454.         }
  455.  
  456.         fputs (ptr, fi);
  457.         fputc ('\n', fi);
  458.     }
  459.  
  460.     if (fclose (fi))
  461.     {
  462. DEFMESSAGE( __write_failed, "%s:\n Write failed!" )
  463.         error (__write_failed, av[0]);
  464.         goto undo;
  465.     } else
  466.     {
  467. DEFMESSAGE( __saving_ok, "Saving OK" )
  468.         title (__saving_ok);
  469.         SETF_MFORCETITLE(ep,TRUE);
  470.  
  471.         /* Only remove modify if blockmode is OFF */
  472.         SETF_MODIFIED(ep, (GETF_MODIFIED(ep) && blockmode));
  473.     }
  474.  
  475.     /* PATCH_NULL 12-08-94 moved here */
  476.     if (GETF_SAVEICONS(ep))       /* PATCH_NULL 12-08-94 replaced Wbs && Wdisable */
  477.     {     /* Write out .info file */
  478.         DISKOBJ sdo, * d;
  479.  
  480.         memset (&sdo, 0, sizeof(sdo));
  481.  
  482.         if ((d = GetDiskObject ((char *)av[1])) == NULL)
  483.         {
  484.         /* PATCH_NULL 15-08-94 ADDED - use any icon */
  485.         char *iconname;
  486.         if ((iconname = getvar("iconname"))) { /* that is a very slow method, but we need not care since we have Disk access following */
  487.             if ((d = GetDiskObject (iconname))) {
  488.             PutDiskObject((char *)av[1], d);
  489.             FreeDiskObject (d);
  490.             } /* if */
  491.             free (iconname);
  492.         } /* if */
  493.         if (d != NULL); /* begin an (empty) "if"-statement to allow the following "else"; we cannot use the previous "if" since that does not make sure, we have an icon found */
  494.         /* PATCH_NULL 15-08-94 ADDED - use any icon */
  495.  
  496.         /* Get path to XDME with name of the editor appended */
  497.         /* WAS: if (Wbs && getpathto (Wbs->sm_ArgList[0].wa_Lock, Wbs->sm_ArgList[0].wa_Name, tmp_buffer)) */
  498.         else if (getappname(tmp_buffer, sizeof(tmp_buffer)))  /* PATCH_NULL 12-08-94 : added */
  499.         {
  500.  
  501.             sdo.do_Magic   = WB_DISKMAGIC;
  502.             sdo.do_Version = WB_DISKVERSION;
  503.  
  504.             makemygadget (&sdo.do_Gadget);
  505.  
  506.             sdo.do_Type        = WBPROJECT;
  507.             sdo.do_DefaultTool = (char *)tmp_buffer;
  508.             sdo.do_ToolTypes   = NULL;
  509.             sdo.do_CurrentX    = NO_ICON_POSITION;
  510.             sdo.do_CurrentY    = NO_ICON_POSITION;
  511.             sdo.do_DrawerData  = NULL;
  512.             sdo.do_ToolWindow  = NULL;
  513.             sdo.do_StackSize   = 8192;
  514.  
  515.             PutDiskObject ((char *)av[1], &sdo);
  516.         }
  517.         } else
  518.         {
  519.         FreeDiskObject (d);
  520.         }
  521.     }
  522.     } else
  523.     {
  524. DEFMESSAGE( __cant_open_write, "%s:\nUnable to open write file\n`%s'" )
  525.     error (__cant_open_write, av[0], av[1]);
  526. undo:
  527.     if (GETF_DOBACK(Ep))
  528.     {
  529.         sprintf (buffer, "%s.bak", av[1]);
  530.         Rename (buffer, av[1]);
  531.     }
  532.     }
  533.  
  534.     blockmode = 0;
  535.     CurrentDir (oldlock);
  536.  
  537.     window_title ();
  538.  
  539.     return (!GETF_ABORTCOMMAND(ep));
  540. } /* do_saveit */
  541.  
  542.  
  543. /*DEFHELP #cmd io CD dir - set directory of current window to @{B}dir@{UB} */
  544.  
  545. DEFUSERCMD("cd", 1, CF_VWM|CF_ICO, void, do_cd, (void),)
  546. {
  547.     BPTR oldlock;
  548.     BPTR lock;
  549.  
  550.     oldlock = CurrentDir (Ep->dirlock);
  551.  
  552.     if (lock = Lock (av[1], SHARED_LOCK))
  553.     {
  554.     UnLock (CurrentDir (oldlock));
  555.     Ep->dirlock = (long)lock;
  556.     } else
  557.     {
  558.     CurrentDir (oldlock);
  559.  
  560. DEFMESSAGE( __cant_cd, "%s:\nUnable to cd to\n`%s'" )
  561.     error (__cant_cd, av[0], av[1]);
  562.     }
  563. } /* do_cd */
  564.  
  565.  
  566. /*DEFHELP #cmd misc,io PRINT text - Print @{B}text@{UB} to the shell XDME was started in */
  567.  
  568. DEFUSERCMD("print", 1, CF_VWM|CF_ICO,void,do_print,(void),)
  569. {
  570.     puts (av[1]);
  571. } /* do_print */
  572.  
  573.  
  574. /******************************************************************************
  575. ***** END io.c
  576. ******************************************************************************/
  577.